class TDT {
	//判断url加上配置的代理
	static getProxyUrl(config) {
		if (!config.proxy || !config.url) return config;
		if (config.url instanceof Cesium.Resource) return config;
		var opts = {};
		for (var key in config) {
			opts[key] = config[key];
		}
		opts.url = new Cesium.Resource({
			url: opts.url,
			proxy: new Cesium.DefaultProxy(opts.proxy)
		});
		return opts;
	}
	static convertImageryLayer(opts){
		var layer;
		var _layer;
		var maxLevel = 18;
		switch (opts.layer) {
			default:
			case "vec_d":
				_layer = "vec";
				break;
			case "vec_z":
				_layer = "cva";
				break;
			case "img_d":
				_layer = "img";
				break;
			case "img_z":
				_layer = "cia";
				break;
			case "ter_d":
				_layer = "ter";
				maxLevel = 14;
				break;
			case "ter_z":
				_layer = "cta";
				maxLevel = 14;
				break;
		}



		var _key=Cesium.defaultValue(opts.key,'87949882c75775b5069a0076357b7530');
		if (opts.crs == "4326" || opts.crs == "EPSG4326" || opts.crs == "EPSG:4326") {
			//wgs84   
			var matrixIds = new Array(maxLevel);
			for (var z = 0; z <= maxLevel; z++) {
				matrixIds[z] = (z + 1).toString();
			}
			var _url = 'https://t{s}.tianditu.gov.cn/' + _layer + '_c/wmts?service=WMTS&version=1.0.0&request=GetTile&tilematrix={TileMatrix}&layer=' + _layer + '&style={style}&tilerow={TileRow}&tilecol={TileCol}&tilematrixset={TileMatrixSet}&format=tiles&tk=' + _key;
			if (opts.proxy) //存在代理时
				_url = (0, this.getProxyUrl)({ url: _url.replace('{s}', '0'), proxy: opts.proxy }).url;

			layer = new Cesium.WebMapTileServiceImageryProvider({
				url: _url,
				layer: _layer,
				style: 'default',
				format: 'tiles',
				tileMatrixSetID: 'c',
				subdomains: ['0', '1', '2', '3', '4', '5', '6', '7'],
				tileMatrixLabels: matrixIds,
				tilingScheme: new Cesium.GeographicTilingScheme(), //WebMercatorTilingScheme、GeographicTilingScheme
				maximumLevel: maxLevel
			});
		} else {
			//墨卡托  
			var matrixIds = new Array(maxLevel);
			for (var z = 0; z <= maxLevel; z++) {
				matrixIds[z] = z.toString();
			}
			var _url = 'https://t{s}.tianditu.gov.cn/' + _layer + '_w/wmts?service=WMTS&version=1.0.0&request=GetTile&tilematrix={TileMatrix}&layer=' + _layer + '&style={style}&tilerow={TileRow}&tilecol={TileCol}&tilematrixset={TileMatrixSet}&format=tiles&tk=' + _key;

			if (opts.proxy) //存在代理时
				_url = (0, this.getProxyUrl)({ url: _url.replace('{s}', '0'), proxy: opts.proxy }).url;
			layer = new Cesium.WebMapTileServiceImageryProvider({
				url: _url,
				layer: _layer,
				style: 'default',
				format: 'tiles',
				tileMatrixSetID: 'w',
				subdomains: ['0', '1', '2', '3', '4', '5', '6', '7'],
				tileMatrixLabels: matrixIds,
				tilingScheme: new Cesium.WebMercatorTilingScheme(),
				maximumLevel: maxLevel
			});
		}
		return layer;
	}
}
export {TDT};